package com.zhong.gang.pda.ui.carorder;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.databinding.LayoutFindBinding;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.pda.api.Api;
import com.zhong.gang.pda.api.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CarOrderAct extends ActBaseRefresh<LayoutFindBinding, OrderNumAdapter>
        implements BaseQuickAdapter.OnItemClickListener , TextWatcher {
    private static OrderListener listener;
    private List<OrderNumBean> dataList = new ArrayList<>();
    public static final void openAct(Context context, OrderListener listener){
        Intent intent = new Intent(context,CarOrderAct.class);
        context.startActivity(intent);
        CarOrderAct.listener = listener;
    }


    @Override
    public int LayoutRes() {
        return R.layout.layout_find;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "选择内容";
    }

    @Override
    public void initView() {
        setAddMore(false);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new OrderNumAdapter(),bind.recyclerView);
        adapter.setOnItemClickListener(this);
        bind.phone.addTextChangedListener(this);
        bind.phone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                /*判断是否是“搜索”键*/
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                   afterTextChanged(null);
                    return true;
                }
                return false;
            }
        });
        bind.find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        HttpUtils.get(Api.getSendCarList,this)
                .addParams("X-HEADER-USER-ID",getIntent().getStringExtra(Config.USER_ID))
                .addParams("current",page+"")
                .addParams("size","10")
                .build().execute(new ActCallBack<BaseDataBean<List<OrderNumBean>>>(){
            @Override
            public void onResponse(BaseDataBean<List<OrderNumBean>> data) throws Exception {
                super.onResponse(data);
                if (page == 1)
                    dataList = data.getResult();
                else dataList.addAll(data.getResult());
                setData(data.getResult());
            }

            @Override
            public void onError(String str) {
                super.onError(str);
                showMainFragemt();
                setData(new ArrayList<OrderNumBean>());
            }
        });

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        OrderNumBean bean = (OrderNumBean) adapter.getItem(position);
        closeActivity();
        if (listener != null)
            listener.onOrderListener(bean);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (bind.phone.getText().toString().endsWith("***###.")){
            try {
                JSONObject object = new JSONObject(bind.phone.getText().toString());
                for (OrderNumBean bean:dataList){
                    if (bean.getId() == object.getInt("id")){
                        List<OrderNumBean> data = new ArrayList<>();
                        data.add(bean);
                        setData(data);
                        return;
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        hideKeyboard();
    }

    public  void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }
}
